package com.sch.hsy.web.controller.system;

import com.sch.hsy.common.annotation.Log;
import com.sch.hsy.common.core.controller.BaseController;
import com.sch.hsy.common.core.domain.AjaxResult;
import com.sch.hsy.common.core.domain.Ztree;
import com.sch.hsy.common.core.domain.entity.SysBook;
import com.sch.hsy.common.core.domain.entity.SysDept;
import com.sch.hsy.common.core.domain.entity.SysRole;
import com.sch.hsy.common.core.domain.entity.SysUser;
import com.sch.hsy.common.core.page.TableDataInfo;
import com.sch.hsy.common.enums.BusinessType;
import com.sch.hsy.common.utils.file.FileUploadUtils;
import com.sch.hsy.system.domain.SysQues;
import com.sch.hsy.system.service.ISysBookService;
import com.sch.hsy.system.service.ISysDeptService;
import com.sch.hsy.system.service.ISysQuesService;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 书籍管理
 *
 */
@Controller
@RequestMapping("/book")
public class SysBookController extends BaseController {

    private String prefix = "book";

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysBookService bookService;

    @Autowired
    private ISysQuesService quesService;

    @Value("${file.upload.path}")
    private String filePath;

    @GetMapping("/book")
    public String book()
    {
        return prefix + "/book";
    }

    /**
     * 加载班级列表树
     */
    @GetMapping("/bjtree")
    @ResponseBody
    public List<Ztree> bjTree()
    {
        List<Ztree> ztrees = deptService.selectDeptByParentId();
        return ztrees;
    }

    /**
     * 选择班级树
     *
     * @param deptId 班级ID
     */
    @GetMapping(value = { "/selectDeptTree/{deptId}"})
    public String selectDeptTree(@PathVariable("deptId") Long deptId,
                                 ModelMap mmap)
    {
        mmap.put("dept", deptService.selectDeptById(deptId));
        return prefix + "/tree";
    }

    /**
     * 获取书籍列表
     * @param book
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysBook book)
    {
        startPage();
        List<SysBook> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 新增书籍
     */
    @GetMapping("/addBook")
    public String addBook(ModelMap mmap)
    {
        return prefix + "/addBook";
    }

    /**
     * 书籍预览
     */
    @GetMapping("/bookView/{id}")
    public String bookView(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysBook sysBook = new SysBook();
        sysBook.setBookId(id);
        SysBook book = bookService.selectBookList(sysBook).get(0);
        book.setFileName("/view/" + book.getFileName());
        mmap.put("book", book);
        return prefix + "/bookView";
    }
    /**
     * 新增信息
     */
    @PostMapping("/addBook")
    @ResponseBody
    public AjaxResult addBook(MultipartFile file, SysBook book) throws IOException {
        if (file != null) {
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            book.setFileName(fileName);
            //获取文件后缀
            String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
            book.setFileSuffix(suffix);
        }
        return toAjax(bookService.insertBook(book));
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long ids)
    {
        SysBook query = new SysBook();
        query.setBookId(ids);
        SysBook sysBook = bookService.selectBookList(query).get(0);

        int result = bookService.deleteBookById(ids);
        if(result > 0){
            File file = new File(filePath + "/" + sysBook.getFileName());
            FileUtils.deleteQuietly(file);
        }
        return toAjax(result);
    }



}
